package com.zzedu.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.zzedu.common.annotation.Log;
import com.zzedu.common.core.controller.BaseController;
import com.zzedu.common.core.domain.AjaxResult;
import com.zzedu.common.core.page.TableDataInfo;
import com.zzedu.common.enums.BusinessType;
import com.zzedu.common.utils.SecurityUtils;
import com.zzedu.common.utils.ServletUtils;
import com.zzedu.common.utils.bean.BeanUtils;
import com.zzedu.common.utils.poi.ExcelUtil;
import com.zzedu.domain.ZzCustomer;
import com.zzedu.dto.ZzCustomerDTO;
import com.zzedu.params.ZzCustomerParams;
import com.zzedu.service.IZzCustomerService;
import com.zzedu.vo.ZzCustomerVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 销售信息Controller
 *
 * @author Mr.Wang
 * @date 2025-05-29
 */
@RestController
@RequestMapping("/saledata/customer")
public class ZzCustomerController extends BaseController {
    @Autowired
    private IZzCustomerService zzCustomerService;

    /**
     * 查询销售信息列表
     */
    @PreAuthorize("@permission.hasAnyPermissions('saledata:customer:query')")
    @RequestMapping("/list")
    public TableDataInfo list(@RequestBody ZzCustomerDTO zzCustomer) {
        IPage<ZzCustomer> page = zzCustomerService.page(zzCustomer.getPageDTO(), zzCustomerService.buildQuery(zzCustomer));
        return getDataTable(page, zzCustomerService.convertVO(page.getRecords()));
    }

    /**
     * 导出销售信息列表
     */
    @PreAuthorize("@permission.hasAnyPermissions('saledata:customer:export')")
    @Log(title = "销售信息", businessType = BusinessType.EXPORT)
    @RequestMapping("/export")
    public void export(ZzCustomerDTO zzCustomer) {
        List<ZzCustomer> list = zzCustomerService.list(zzCustomerService.buildQuery(zzCustomer));
        ExcelUtil<ZzCustomer> util = new ExcelUtil<ZzCustomer>(ZzCustomer.class);
        util.exportExcel(ServletUtils.getResponse(), list, "销售信息数据");
    }


    /**
     * 新增保存销售信息
     */
    @PreAuthorize("@permission.hasAnyPermissions('saledata:customer:add')")
    @Log(title = "销售信息", businessType = BusinessType.INSERT)
    @RequestMapping("/add")
    public AjaxResult addSave(@Validated @RequestBody ZzCustomerParams zzCustomerParams) {
        ZzCustomer zzCustomer = new ZzCustomer();
        String[] provinceAndCityAndDistrict = zzCustomerParams.getProvinceAndCityAndDistrict();
        if (null != provinceAndCityAndDistrict && provinceAndCityAndDistrict.length > 0) {
            zzCustomerParams.setProvince(provinceAndCityAndDistrict[0]);
            if (provinceAndCityAndDistrict.length > 1) {
                zzCustomerParams.setCity(provinceAndCityAndDistrict[1]);
            }
            if (provinceAndCityAndDistrict.length > 2) {
                zzCustomerParams.setDistrict(provinceAndCityAndDistrict[2]);
            }
        }
        BeanUtils.copyBeanProp(zzCustomer, zzCustomerParams);
        zzCustomer.setDeptId(SecurityUtils.getSysUser().getDeptId());

        return toAjax(zzCustomerService.save(zzCustomer));
    }


    /**
     * 获取销售信息详细信息
     */
    @PreAuthorize("@permission.hasAnyPermissions('saledata:customer:query')")
    @RequestMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        ZzCustomerVO customerVO = zzCustomerService.convertVO(Lists.newArrayList(zzCustomerService.getById(id))).get(0);
        List<String> provinceAndCityAndDistrict = Lists.newArrayList(customerVO.getProvince(), customerVO.getCity(), customerVO.getDistrict());
        JSONObject jsonObject = (JSONObject) JSON.toJSON(customerVO);
        jsonObject.put("provinceAndCityAndDistrict", provinceAndCityAndDistrict);
        jsonObject.remove("deleted");
        jsonObject.remove("gmtCreate");
        jsonObject.remove("gmtModify");
        jsonObject.remove("createBy");
        jsonObject.remove("updateBy");
        return success(jsonObject);
    }


    /**
     * 修改保存销售信息
     */
    @PreAuthorize("@permission.hasAnyPermissions('saledata:customer:edit')")
    @Log(title = "销售信息", businessType = BusinessType.UPDATE)
    @RequestMapping("/edit")
    public AjaxResult editSave(@Validated @RequestBody ZzCustomerParams zzCustomerParams) {
        ZzCustomer zzCustomer = new ZzCustomer();
        String[] provinceAndCityAndDistrict = zzCustomerParams.getProvinceAndCityAndDistrict();
        if (null != provinceAndCityAndDistrict && provinceAndCityAndDistrict.length > 0) {
            zzCustomerParams.setProvince(provinceAndCityAndDistrict[0]);
            if (provinceAndCityAndDistrict.length > 1) {
                zzCustomerParams.setCity(provinceAndCityAndDistrict[1]);
            }
            if (provinceAndCityAndDistrict.length > 2) {
                zzCustomerParams.setDistrict(provinceAndCityAndDistrict[2]);
            }
        }
        BeanUtils.copyBeanProp(zzCustomer, zzCustomerParams);
        return toAjax(zzCustomerService.updateById(zzCustomer));
    }

    /**
     * 删除销售信息
     */
    @PreAuthorize("@permission.hasAnyPermissions('saledata:customer:remove')")
    @Log(title = "销售信息", businessType = BusinessType.DELETE)
    @RequestMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(zzCustomerService.removeBatchByIds(ids));
    }


    /**
     * 查询销售单统计列表
     */
    @PreAuthorize("@permission.hasAnyPermissions('saledata:customer:query')")
    @RequestMapping("/statistic")
    public TableDataInfo statistic() {
        return getDataTable(zzCustomerService.statistic());
    }

}
